package com.xy.iot.client.config;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.xy.iot.common.utils.FastJsonUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;
/**
 * @author: niuniu
 * @Date: 2024/6/16 21:21
 * @Description: 能够配置多个HttpMessageConverter
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    /**
     *      ALL = new MediaType("*", "*");
     * 		APPLICATION_ATOM_XML = new MediaType("application", "atom+xml");
     * 		APPLICATION_CBOR = new MediaType("application", "cbor");
     * 		APPLICATION_FORM_URLENCODED = new MediaType("application", "x-www-form-urlencoded");
     * 		APPLICATION_JSON = new MediaType("application", "json");
     * 		APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
     * 		APPLICATION_NDJSON = new MediaType("application", "x-ndjson");
     * 		APPLICATION_OCTET_STREAM = new MediaType("application", "octet-stream");
     * 		APPLICATION_PDF = new MediaType("application", "pdf");
     * 		APPLICATION_PROBLEM_JSON = new MediaType("application", "problem+json");
     * 		APPLICATION_PROBLEM_JSON_UTF8 = new MediaType("application", "problem+json", StandardCharsets.UTF_8);
     * 		APPLICATION_PROBLEM_XML = new MediaType("application", "problem+xml");
     * 		APPLICATION_RSS_XML = new MediaType("application", "rss+xml");
     * 		APPLICATION_STREAM_JSON = new MediaType("application", "stream+json");
     * 		APPLICATION_XHTML_XML = new MediaType("application", "xhtml+xml");
     * 		APPLICATION_XML = new MediaType("application", "xml");
     * 		IMAGE_GIF = new MediaType("image", "gif");
     * 		IMAGE_JPEG = new MediaType("image", "jpeg");
     * 		IMAGE_PNG = new MediaType("image", "png");
     * 		MULTIPART_FORM_DATA = new MediaType("multipart", "form-data");
     * 		MULTIPART_MIXED = new MediaType("multipart", "mixed");
     * 		MULTIPART_RELATED = new MediaType("multipart", "related");
     * 		TEXT_EVENT_STREAM = new MediaType("text", "event-stream");
     * 		TEXT_HTML = new MediaType("text", "html");
     * 		TEXT_MARKDOWN = new MediaType("text", "markdown");
     * 		TEXT_PLAIN = new MediaType("text", "plain");
     * 		TEXT_XML = new MediaType("text", "xml");
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
        // 清除默认json转换器
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        //升级最新版本需加=============================================================
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_STREAM_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_PROBLEM_XML);
        FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
        httpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(FastJsonUtil.features);
        config.setSerializeConfig(FastJsonUtil.config);
        httpMessageConverter.setFastJsonConfig(config);
        converters.add(httpMessageConverter);
    }
}
